package engine;

/**
 * 
 * A 2D line, stored using many different methods, so that many different line
 * operations can be processed at maximum speed.
 * 
 * @author dylan
 *
 */
public class Line {
	public Vector p1, p2;
	private Vector connect;
	private double slope;
	public double a, b, c, d; // For intersection testing

	public Line(Vector p1, Vector p2) {
		this.p1 = p1;
		this.p2 = p2;
		connect = p2.subtract(p1);
		Vector direction = p2.subtract(p1).normalize();
		
		/*
		 * These numbers do SO MUCH
		 */
		a = - direction.getY();
		b = direction.getX();
		d = a * p1.getX() + b * p1.getY();

		slope = connect.getY() / connect.getX();
	}

	/**
	 * Find the intersection of the two lines using Cramer's Rule.
	 * 
	 * @param line
	 * @return
	 */
	public Vector intersection(Line line) {
		return new Vector((line.b * d - b * line.d) / (a * line.b - line.a * b),
						  (a * line.d - line.a * d) / (a * line.b - line.a * b));
	}
	
	/**
	 * Returns whether or not the point falls within the bounding box of the line.
	 * 
	 * @param p
	 * @return
	 */
	public boolean pointInLineRectangle(Vector p){
		return (p.getX() > p1.getX() && p.getX() < p2.getX() &&
				p.getY() > p1.getY() && p.getY() < p2.getY());
	}
	
	/**
	 * Returns the minimum distance from the line to the vector
	 * 
	 * @param p
	 * @return
	 */
	public double distanceToLine(Vector p){
		return a * p.getX() + b * p.getY() - d;
	}
}





